 /*
 * Copyright (c) 2010, James Daniello
 * This project is licensed under the 'New BSD' license.
 * The full text of the license can be found in the root
 * directory under the filename "LICENSE"
 */
package src.gui;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Container;
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.text.NumberFormat;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import src.network.PICAIURecruittedUnit;

public class PICAIUClientGUIPreGame implements PICAIUGUI {

  //The various gui components.
  private Container frame;       //<-- the main panel container
  private JPanel content, main;
  private PICAIUClientGUI parent;
  String enemyName = "";
  //gui elements
  private JLabel myInfo;
  private JLabel enemyInfo;
  private JPanel myInfoCont, padding;
  private JLabel gunnerLabel, tankLabel, rocketLabel;
  private JPanel gunnerPanel, tankPanel, rocketPanel;
  private JTextField gunnerBuy, tankBuy, rocketBuy;
  private JPanel gunnerButtons, tankButtons, rocketButtons;
  private JButton gunnerPlus, gunnerMinus, tankPlus, tankMinus, rocketPlus, rocketMinus;
  private JButton gunnerRecruit, tankRecruit, rocketRecruit;
  private JPanel gunPad, tankPad, rocPad;
  private JPanel gunnerPadding, tankPadding, rocketPadding;
  private JScrollPane gunnerScrollPane, tankScrollPane, rocketScrollPane;
  private JListWithImages gunnerImgs, tankImgs, rocketImgs;
  private Vector gunnerVec = new Vector();
  private Vector tankVec = new Vector();
  private Vector rocketVec = new Vector();
  private NumberFormat numFormat = NumberFormat.getIntegerInstance();
  //private PICAIURecruittedUnit[] gunners = new PICAIURecruittedUnit[25];
  private ConcurrentHashMap<String, PICAIURecruittedUnit> gunnerMen = new ConcurrentHashMap<String, PICAIURecruittedUnit>();
  //private PICAIURecruittedUnit[] tanks = new PICAIURecruittedUnit[25];
  private ConcurrentHashMap<String, PICAIURecruittedUnit> tankMen = new ConcurrentHashMap<String, PICAIURecruittedUnit>();
  //private PICAIURecruittedUnit[] rocketeers = new PICAIURecruittedUnit[25];
  private ConcurrentHashMap<String, PICAIURecruittedUnit> rocketMen = new ConcurrentHashMap<String, PICAIURecruittedUnit>();
  private JPanel bottom;
  private JButton readyUp, cancel;
  private JLabel gunnerStats1, tankStats1, rocketStats1,
          gunnerStats2, tankStats2, rocketStats2,
          gunnerStats3, tankStats3, rocketStats3;
  private JPanel gunnerDelPanel, tankDelPanel, rocketDelPanel, tankStatsPadding;
  private JButton gunnerDel, tankDel, rocketDel;

  /**
   * Creates the gui component for when the server can not be contacted
   */
  public PICAIUClientGUIPreGame(Container frameIn, PICAIUClientGUI parentIn) {
    frame = frameIn;
    parent = parentIn;
    initGUI();
  }

  /**
   * creates the gui components
   */
  public void initGUI() {
    content = new JPanel(new BorderLayout());

    myInfoCont = new JPanel(new BorderLayout());
    myInfoCont.setMinimumSize(new java.awt.Dimension(245, 50));
    myInfoCont.setPreferredSize(new java.awt.Dimension(245, 50));
    myInfoCont.setMaximumSize(new java.awt.Dimension(245, 50));
    myInfoCont.setSize(new java.awt.Dimension(245, 50));
    myInfo = new JLabel("      [loading]");
    enemyInfo = new JLabel("Awaiting Other Player...      ");
    myInfoCont.add(myInfo, BorderLayout.WEST);
    myInfoCont.add(enemyInfo, BorderLayout.EAST);

    padding = new JPanel();
    padding.setMinimumSize(new java.awt.Dimension(780, 6));
    padding.setPreferredSize(new java.awt.Dimension(780, 6));
    padding.setMaximumSize(new java.awt.Dimension(780, 6));
    padding.setSize(new java.awt.Dimension(780, 6));

    myInfoCont.add(padding, BorderLayout.SOUTH);

    content.add(myInfoCont, BorderLayout.NORTH);



    main = new JPanel();
    content.add(main, BorderLayout.CENTER);


    //gunners
    gunnerPanel = new JPanel(new FlowLayout());
    gunnerLabel = new JLabel("Gunners:       ");
    gunnerPanel.add(gunnerLabel);
    gunnerBuy = new JFormattedTextField(numFormat);
    gunnerBuy.setColumns(5);
    gunnerBuy.setText("10");
    gunnerPanel.add(gunnerBuy);
    gunnerButtons = new JPanel(new BorderLayout());
    gunnerPlus = new JButton(" + ");
    gunnerPlus.setFont(frame.getFont().deriveFont(8f));
    gunnerPlus.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        if (gunnerBuy.getText().equals("")) {
          return;
        }
        int amount = Integer.parseInt(gunnerBuy.getText().replaceAll(",", ""));
        if (amount < parent.getUserMoney()) {
          amount++;
          gunnerBuy.setText("" + amount);
        }
      }
    });
    gunnerMinus = new JButton(" - ");
    gunnerMinus.setFont(frame.getFont().deriveFont(8f));
    gunnerMinus.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        if (gunnerBuy.getText().equals("")) {
          return;
        }
        int amount = Integer.parseInt(gunnerBuy.getText().replaceAll(",", ""));
        if (amount > 0) {
          amount--;
          gunnerBuy.setText("" + amount);
        }
      }
    });
    gunnerButtons.add(gunnerPlus, BorderLayout.NORTH);
    gunnerButtons.add(gunnerMinus, BorderLayout.SOUTH);
    gunnerPanel.add(gunnerButtons);
    gunnerRecruit = new JButton(" Recruit ");
    gunnerRecruit.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        if (gunnerBuy.getText().equals("")) {
          return;
        }
        int amount = Integer.parseInt(gunnerBuy.getText().replaceAll(",", ""));
        if (amount == 0) {
          return;
        } else {
          parent.getClient().sendDataToServer("REC#GUN#" + amount);
        }
      }
    });
    gunnerPanel.add(gunnerRecruit);
    gunPad = new JPanel();
    gunPad.setMinimumSize(new java.awt.Dimension(570, 40));
    gunPad.setPreferredSize(new java.awt.Dimension(570, 40));
    gunPad.setMaximumSize(new java.awt.Dimension(570, 40));
    gunPad.setSize(new java.awt.Dimension(570, 40));
    gunnerPanel.add(gunPad);


    gunnerPadding = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 1));
    gunnerPadding.setMinimumSize(new java.awt.Dimension(180, 60));
    gunnerPadding.setPreferredSize(new java.awt.Dimension(180, 60));
    gunnerPadding.setMaximumSize(new java.awt.Dimension(180, 60));
    gunnerPadding.setSize(new java.awt.Dimension(180, 60));


    gunnerDel = new JButton(" X ");
    gunnerDel.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        if (!gunnerImgs.isSelectionEmpty()) {
          sellBack(gunnerMen.get(((JPanel) gunnerImgs.getSelectedValue()).getToolTipText()));
        }
      }
    });
    gunnerDel.setFont(frame.getFont().deriveFont(8f));
    gunnerDelPanel = new JPanel(new BorderLayout());
    gunnerDelPanel.setMinimumSize(new java.awt.Dimension(170, 10));
    gunnerDelPanel.setPreferredSize(new java.awt.Dimension(170, 10));
    gunnerDelPanel.setMaximumSize(new java.awt.Dimension(170, 10));
    gunnerDelPanel.setSize(new java.awt.Dimension(170, 10));
    gunnerDelPanel.add(gunnerDel, BorderLayout.EAST);

    gunnerStats1 = new JLabel();
    gunnerStats1.setFont(frame.getFont().deriveFont(9f));
    gunnerStats2 = new JLabel();
    gunnerStats2.setFont(frame.getFont().deriveFont(9f));
    gunnerStats3 = new JLabel();
    gunnerStats3.setFont(frame.getFont().deriveFont(9f));
    gunnerPadding.add(gunnerDelPanel, BorderLayout.NORTH);
    gunnerPadding.add(gunnerStats1, BorderLayout.CENTER);
    gunnerPadding.add(gunnerStats2, BorderLayout.CENTER);
    gunnerPadding.add(gunnerStats3, BorderLayout.CENTER);


    gunnerImgs = new JListWithImages();
    gunnerImgs.addListSelectionListener(new ListSelectionListener() {

      public void valueChanged(ListSelectionEvent e) {
        if (gunnerImgs.isSelectionEmpty()) {
          return;
        }
        rocketImgs.clearSelection();
        tankImgs.clearSelection();
        gunnerStats1.setText(((PICAIURecruittedUnit) gunnerMen.get(((JPanel) gunnerImgs.getSelectedValue()).getToolTipText())).getFormattedInfo1());
        gunnerStats2.setText(((PICAIURecruittedUnit) gunnerMen.get(((JPanel) gunnerImgs.getSelectedValue()).getToolTipText())).getFormattedInfo2());
        gunnerStats3.setText(((PICAIURecruittedUnit) gunnerMen.get(((JPanel) gunnerImgs.getSelectedValue()).getToolTipText())).getFormattedInfo3());
      }
    });
    gunnerImgs.addKeyListener(new KeyListener() {

      public void keyTyped(KeyEvent e) {
      }

      public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == 127) {
          if (!gunnerImgs.isSelectionEmpty()) {
            sellBack(gunnerMen.get(((JPanel) gunnerImgs.getSelectedValue()).getToolTipText()));
          }
        }
      }

      public void keyReleased(KeyEvent e) {
      }
    });
    gunnerScrollPane = new JScrollPane(gunnerImgs);
    gunnerScrollPane.setMinimumSize(new java.awt.Dimension(400, 60));
    gunnerScrollPane.setPreferredSize(new java.awt.Dimension(400, 60));
    gunnerScrollPane.setMaximumSize(new java.awt.Dimension(400, 60));
    gunnerScrollPane.setSize(new java.awt.Dimension(400, 60));
    gunnerScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    gunnerScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    //gunnerPadding.add(gunnerScrollPane);




    //tanks
    tankPanel = new JPanel();
    tankLabel = new JLabel("Tanks:           ");
    tankPanel.add(tankLabel);
    tankBuy = new JFormattedTextField(numFormat);
    tankBuy.setColumns(5);
    tankBuy.setText("20");
    tankPanel.add(tankBuy);
    tankButtons = new JPanel(new BorderLayout());
    tankPlus = new JButton(" + ");
    tankPlus.setFont(frame.getFont().deriveFont(8f));
    tankPlus.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        if (tankBuy.getText().equals("")) {
          return;
        }
        int amount = Integer.parseInt(tankBuy.getText().replaceAll(",", ""));
        if (amount < parent.getUserMoney()) {
          amount++;
          tankBuy.setText("" + amount);
        }
      }
    });
    tankMinus = new JButton(" - ");
    tankMinus.setFont(frame.getFont().deriveFont(8f));
    tankMinus.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        if (tankBuy.getText().equals("")) {
          return;
        }
        int amount = Integer.parseInt(tankBuy.getText().replaceAll(",", ""));
        if (amount > 0) {
          amount--;
          tankBuy.setText("" + amount);
        }
      }
    });
    tankButtons.add(tankPlus, BorderLayout.NORTH);
    tankButtons.add(tankMinus, BorderLayout.SOUTH);
    tankPanel.add(tankButtons);
    tankRecruit = new JButton(" Recruit ");
    tankRecruit.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        if (tankBuy.getText().equals("")) {
          return;
        }
        int amount = Integer.parseInt(tankBuy.getText().replaceAll(",", ""));
        if (amount == 0) {
          return;
        } else {
          parent.getClient().sendDataToServer("REC#TAN#" + amount);
        }
      }
    });
    tankPanel.add(tankRecruit);
    tankPad = new JPanel();
    tankPad.setMinimumSize(new java.awt.Dimension(570, 40));
    tankPad.setPreferredSize(new java.awt.Dimension(570, 40));
    tankPad.setMaximumSize(new java.awt.Dimension(570, 40));
    tankPad.setSize(new java.awt.Dimension(570, 40));
    tankPanel.add(tankPad);

    tankPadding = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 1));
    tankPadding.setMinimumSize(new java.awt.Dimension(180, 90));
    tankPadding.setPreferredSize(new java.awt.Dimension(180, 90));
    tankPadding.setMaximumSize(new java.awt.Dimension(180, 90));
    tankPadding.setSize(new java.awt.Dimension(180, 90));

    tankDel = new JButton(" X ");
    tankDel.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        if (!tankImgs.isSelectionEmpty()) {
          sellBack(tankMen.get(((JPanel) tankImgs.getSelectedValue()).getToolTipText()));
        }
      }
    });
    tankDel.setFont(frame.getFont().deriveFont(8f));
    tankDelPanel = new JPanel(new BorderLayout());
    tankDelPanel.setMinimumSize(new java.awt.Dimension(170, 10));
    tankDelPanel.setPreferredSize(new java.awt.Dimension(170, 10));
    tankDelPanel.setMaximumSize(new java.awt.Dimension(170, 10));
    tankDelPanel.setSize(new java.awt.Dimension(170, 10));
    tankDelPanel.add(tankDel, BorderLayout.EAST);

    tankStats1 = new JLabel();
    tankStats1.setFont(frame.getFont().deriveFont(9f));
    tankStats2 = new JLabel();
    tankStats2.setFont(frame.getFont().deriveFont(9f));
    tankStats3 = new JLabel();
    tankStats3.setFont(frame.getFont().deriveFont(9f));
    tankPadding.add(tankDelPanel, BorderLayout.NORTH);
    tankStatsPadding = new JPanel();
    tankStatsPadding.setMinimumSize(new java.awt.Dimension(120, 12));
    tankStatsPadding.setPreferredSize(new java.awt.Dimension(120, 12));
    tankStatsPadding.setMaximumSize(new java.awt.Dimension(120, 12));
    tankStatsPadding.setSize(new java.awt.Dimension(120, 12));
    tankPadding.add(tankStatsPadding, BorderLayout.CENTER);
    tankPadding.add(tankStats1, BorderLayout.CENTER);
    tankPadding.add(tankStats2, BorderLayout.CENTER);
    tankPadding.add(tankStats3, BorderLayout.CENTER);

    tankImgs = new JListWithImages();
    tankImgs.addListSelectionListener(new ListSelectionListener() {

      public void valueChanged(ListSelectionEvent e) {
        if (tankImgs.isSelectionEmpty()) {
          return;
        }
        rocketImgs.clearSelection();
        gunnerImgs.clearSelection();
        tankStats1.setText(((PICAIURecruittedUnit) tankMen.get(((JPanel) tankImgs.getSelectedValue()).getToolTipText())).getFormattedInfo1());
        tankStats2.setText(((PICAIURecruittedUnit) tankMen.get(((JPanel) tankImgs.getSelectedValue()).getToolTipText())).getFormattedInfo2());
        tankStats3.setText(((PICAIURecruittedUnit) tankMen.get(((JPanel) tankImgs.getSelectedValue()).getToolTipText())).getFormattedInfo3());
      }
    });
    tankImgs.addKeyListener(new KeyListener() {

      public void keyTyped(KeyEvent e) {
      }

      public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == 127) {
          if (!tankImgs.isSelectionEmpty()) {
            sellBack(tankMen.get(((JPanel) tankImgs.getSelectedValue()).getToolTipText()));
          }
        }
      }

      public void keyReleased(KeyEvent e) {
      }
    });
    tankScrollPane = new JScrollPane(tankImgs);
    tankScrollPane.setMinimumSize(new java.awt.Dimension(400, 90));
    tankScrollPane.setPreferredSize(new java.awt.Dimension(400, 90));
    tankScrollPane.setMaximumSize(new java.awt.Dimension(400, 90));
    tankScrollPane.setSize(new java.awt.Dimension(400, 90));
    tankScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    tankScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    //  tankPadding.add(tankScrollPane);

    //rocketeers
    rocketPanel = new JPanel();
    rocketLabel = new JLabel("Rocketeers: ");
    rocketPanel.add(rocketLabel);
    rocketBuy = new JFormattedTextField(numFormat);
    rocketBuy.setColumns(5);
    rocketBuy.setText("15");
    rocketPanel.add(rocketBuy);
    rocketButtons = new JPanel(new BorderLayout());
    rocketPlus = new JButton(" + ");
    rocketPlus.setFont(frame.getFont().deriveFont(8f));
    rocketPlus.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        if (rocketBuy.getText().equals("")) {
          return;
        }
        int amount = Integer.parseInt(rocketBuy.getText().replaceAll(",", ""));
        if (amount < parent.getUserMoney()) {
          amount++;
          rocketBuy.setText("" + amount);
        }
      }
    });
    rocketMinus = new JButton(" - ");
    rocketMinus.setFont(frame.getFont().deriveFont(8f));
    rocketMinus.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        if (rocketBuy.getText().equals("")) {
          return;
        }
        int amount = Integer.parseInt(rocketBuy.getText().replaceAll(",", ""));
        if (amount > 0) {
          amount--;
          rocketBuy.setText("" + amount);
        }
      }
    });
    rocketButtons.add(rocketPlus, BorderLayout.NORTH);
    rocketButtons.add(rocketMinus, BorderLayout.SOUTH);
    rocketPanel.add(rocketButtons);
    rocketRecruit = new JButton(" Recruit ");
    rocketRecruit.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        if (rocketBuy.getText().equals("")) {
          return;
        }
        int amount = Integer.parseInt(rocketBuy.getText().replaceAll(",", ""));
        if (amount == 0) {
          return;
        } else {
          parent.getClient().sendDataToServer("REC#ROC#" + amount);
        }
      }
    });
    rocketPanel.add(rocketRecruit);
    rocPad = new JPanel();
    rocPad.setMinimumSize(new java.awt.Dimension(570, 40));
    rocPad.setPreferredSize(new java.awt.Dimension(570, 40));
    rocPad.setMaximumSize(new java.awt.Dimension(570, 40));
    rocPad.setSize(new java.awt.Dimension(570, 40));
    rocketPanel.add(rocPad);


    rocketPadding = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 1));
    rocketPadding.setMinimumSize(new java.awt.Dimension(180, 60));
    rocketPadding.setPreferredSize(new java.awt.Dimension(180, 60));
    rocketPadding.setMaximumSize(new java.awt.Dimension(180, 60));
    rocketPadding.setSize(new java.awt.Dimension(180, 60));


    rocketDel = new JButton(" X ");
    rocketDel.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        if (!rocketImgs.isSelectionEmpty()) {
          sellBack(rocketMen.get(((JPanel) rocketImgs.getSelectedValue()).getToolTipText()));
        }
      }
    });
    rocketDel.setFont(frame.getFont().deriveFont(8f));
    rocketDelPanel = new JPanel(new BorderLayout());
    rocketDelPanel.setMinimumSize(new java.awt.Dimension(170, 10));
    rocketDelPanel.setPreferredSize(new java.awt.Dimension(170, 10));
    rocketDelPanel.setMaximumSize(new java.awt.Dimension(170, 10));
    rocketDelPanel.setSize(new java.awt.Dimension(170, 10));
    rocketDelPanel.add(rocketDel, BorderLayout.EAST);

    rocketStats1 = new JLabel();
    rocketStats1.setFont(frame.getFont().deriveFont(9f));
    rocketStats2 = new JLabel();
    rocketStats2.setFont(frame.getFont().deriveFont(9f));
    rocketStats3 = new JLabel();
    rocketStats3.setFont(frame.getFont().deriveFont(9f));
    rocketPadding.add(rocketDelPanel, BorderLayout.NORTH);
    rocketPadding.add(rocketStats1, BorderLayout.CENTER);
    rocketPadding.add(rocketStats2, BorderLayout.CENTER);
    rocketPadding.add(rocketStats3, BorderLayout.CENTER);


    rocketImgs = new JListWithImages();
    rocketImgs.addListSelectionListener(new ListSelectionListener() {

      public void valueChanged(ListSelectionEvent e) {
        if (rocketImgs.isSelectionEmpty()) {
          return;
        }
        tankImgs.clearSelection();
        gunnerImgs.clearSelection();
        rocketStats1.setText(((PICAIURecruittedUnit) rocketMen.get(((JPanel) rocketImgs.getSelectedValue()).getToolTipText())).getFormattedInfo1());
        rocketStats2.setText(((PICAIURecruittedUnit) rocketMen.get(((JPanel) rocketImgs.getSelectedValue()).getToolTipText())).getFormattedInfo2());
        rocketStats3.setText(((PICAIURecruittedUnit) rocketMen.get(((JPanel) rocketImgs.getSelectedValue()).getToolTipText())).getFormattedInfo3());
      }
    });
    rocketImgs.addKeyListener(new KeyListener() {

      public void keyTyped(KeyEvent e) {
      }

      public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == 127) {
          if (!rocketImgs.isSelectionEmpty()) {
            sellBack(rocketMen.get(((JPanel) rocketImgs.getSelectedValue()).getToolTipText()));
          }
        }
      }

      public void keyReleased(KeyEvent e) {
      }
    });

    rocketScrollPane =
            new JScrollPane(rocketImgs);
    rocketScrollPane.setMinimumSize(new java.awt.Dimension(400, 60));
    rocketScrollPane.setPreferredSize(new java.awt.Dimension(400, 60));
    rocketScrollPane.setMaximumSize(new java.awt.Dimension(400, 60));
    rocketScrollPane.setSize(new java.awt.Dimension(400, 60));
    rocketScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    rocketScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    //rocketPadding.add(rocketScrollPane);


    main.add(gunnerPanel);
    main.add(gunnerScrollPane);
    main.add(gunnerPadding);
    main.add(tankPanel);
    main.add(tankScrollPane);
    main.add(tankPadding);
    main.add(rocketPanel);
    main.add(rocketScrollPane);
    main.add(rocketPadding);

    bottom = new JPanel(new BorderLayout());

    cancel = new JButton(" Cancel ");
    cancel.setFont(frame.getFont().deriveFont(20f));
    cancel.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        parent.getClient().sendDataToServer("LEFTPREGM");
        clearUnits();
        parent.setGUI("lobby");
        enablePreGame();
      }
    });

    readyUp = new JButton(" Ready! ");
    readyUp.setFont(frame.getFont().deriveFont(20f));
    readyUp.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        if (gunnerMen.isEmpty() && rocketMen.isEmpty() && tankMen.isEmpty()) {
          return;


        }
        gunnerRecruit.setEnabled(false);
        tankRecruit.setEnabled(false);
        rocketRecruit.setEnabled(false);
        readyUp.setEnabled(false);
        gunnerBuy.setEnabled(false);
        gunnerPlus.setEnabled(false);
        gunnerMinus.setEnabled(false);
        tankBuy.setEnabled(false);
        tankPlus.setEnabled(false);
        tankMinus.setEnabled(false);
        rocketBuy.setEnabled(false);
        rocketPlus.setEnabled(false);
        rocketMinus.setEnabled(false);
        parent.getClient().sendDataToServer("READY");


      }
    });
    bottom.add(cancel, BorderLayout.WEST);
    bottom.add(readyUp, BorderLayout.EAST);




  }

  private void sellBack(PICAIURecruittedUnit unit) {
    parent.getClient().sendDataToServer("SELL#" + unit.getID());


  }

  public void enablePreGame() {

    gunnerRecruit.setEnabled(true);
    tankRecruit.setEnabled(true);
    rocketRecruit.setEnabled(true);
    gunnerBuy.setEnabled(true);
    gunnerPlus.setEnabled(true);
    gunnerMinus.setEnabled(true);
    tankBuy.setEnabled(true);
    tankPlus.setEnabled(true);
    tankMinus.setEnabled(true);
    rocketBuy.setEnabled(true);
    rocketPlus.setEnabled(true);
    rocketMinus.setEnabled(true);
    readyUp.setEnabled(true);
    //gunnerImgs.setEnabled(true);
    //rocketImgs.setEnabled(true);
    //tankImgs.setEnabled(true);


  }

  /**
   * This method switches the clients main container to this GUI's graphics.
   */
  @Override
  public void setAsGUI() {

    frame.removeAll();
    frame.add(content, BorderLayout.CENTER);
    frame.add(bottom, BorderLayout.SOUTH);
    frame.validate();



  }

  /**
   * This sets the colors of all the components affected by the color scheme to that of the current
   * colors for the GUI
   */
  @Override
  public void setColors(PICAIUGUIScheme colorScheme) {

    Color colorBG01 = colorScheme.getBG01();
    Color colorBG02 = colorScheme.getBG02();
    Color colorFG01 = colorScheme.getFG01();
    Color colorBorder01 = colorScheme.getBorder01();

    content.setBackground(colorBG01);
    content.setForeground(colorFG01);
    main.setBackground(colorBG01);
    main.setForeground(colorFG01);
    myInfo.setBackground(colorBG01);
    myInfo.setForeground(colorFG01);
    enemyInfo.setBackground(colorBG01);
    enemyInfo.setForeground(colorFG01);
    myInfoCont.setBackground(colorBG01);
    myInfoCont.setForeground(colorFG01);
    padding.setBackground(colorBG02);
    padding.setForeground(colorFG01);
    tankStatsPadding.setBackground(colorBG01);
    tankStatsPadding.setForeground(colorFG01);
    gunnerPanel.setBackground(colorBG01);
    gunnerPanel.setForeground(colorFG01);
    gunnerLabel.setBackground(colorBG01);
    gunnerLabel.setForeground(colorFG01);
    gunnerBuy.setBackground(colorBG02);
    gunnerBuy.setForeground(colorFG01);
    gunnerBuy.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    gunnerButtons.setBackground(colorBG01);
    gunnerButtons.setForeground(colorFG01);
    gunnerPlus.setBackground(colorBG01);
    gunnerPlus.setForeground(colorFG01);
    gunnerPlus.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    gunnerMinus.setBackground(colorBG01);
    gunnerMinus.setForeground(colorFG01);
    gunnerMinus.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    gunnerRecruit.setBackground(colorBG01);
    gunnerRecruit.setForeground(colorFG01);
    gunnerRecruit.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    gunPad.setBackground(colorBG01);
    gunPad.setForeground(colorFG01);
    gunnerScrollPane.setBackground(colorBG01);
    gunnerScrollPane.setForeground(colorFG01);
    gunnerScrollPane.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    gunnerPadding.setBackground(colorBG01);
    gunnerPadding.setForeground(colorFG01);
    gunnerPadding.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    tankPanel.setBackground(colorBG01);
    tankPanel.setForeground(colorFG01);
    tankLabel.setBackground(colorBG01);
    tankLabel.setForeground(colorFG01);
    tankBuy.setBackground(colorBG02);
    tankBuy.setForeground(colorFG01);
    tankBuy.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    tankButtons.setBackground(colorBG01);
    tankButtons.setForeground(colorFG01);
    tankPlus.setBackground(colorBG01);
    tankPlus.setForeground(colorFG01);
    tankPlus.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    tankMinus.setBackground(colorBG01);
    tankMinus.setForeground(colorFG01);
    tankMinus.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    tankRecruit.setBackground(colorBG01);
    tankRecruit.setForeground(colorFG01);
    tankRecruit.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    tankPad.setBackground(colorBG01);
    tankPad.setForeground(colorFG01);
    tankScrollPane.setBackground(colorBG01);
    tankScrollPane.setForeground(colorFG01);
    tankScrollPane.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    tankPadding.setBackground(colorBG01);
    tankPadding.setForeground(colorFG01);
    tankPadding.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    rocketPanel.setBackground(colorBG01);
    rocketPanel.setForeground(colorFG01);
    rocketLabel.setBackground(colorBG01);
    rocketLabel.setForeground(colorFG01);
    rocketBuy.setBackground(colorBG02);
    rocketBuy.setForeground(colorFG01);
    rocketBuy.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    rocketButtons.setBackground(colorBG01);
    rocketButtons.setForeground(colorFG01);
    rocketPlus.setBackground(colorBG01);
    rocketPlus.setForeground(colorFG01);
    rocketPlus.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    rocketMinus.setBackground(colorBG01);
    rocketMinus.setForeground(colorFG01);
    rocketMinus.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    rocketRecruit.setBackground(colorBG01);
    rocketRecruit.setForeground(colorFG01);
    rocketRecruit.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    rocPad.setBackground(colorBG01);
    rocPad.setForeground(colorFG01);
    rocketScrollPane.setBackground(colorBG01);
    rocketScrollPane.setForeground(colorFG01);
    rocketScrollPane.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    rocketPadding.setBackground(colorBG01);
    rocketPadding.setForeground(colorFG01);
    rocketPadding.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));


    gunnerStats1.setForeground(colorFG01);
    gunnerStats2.setForeground(colorFG01);
    gunnerStats3.setForeground(colorFG01);
    gunnerDelPanel.setBackground(colorBG01);
    gunnerDelPanel.setForeground(colorFG01);
    gunnerDel.setBackground(colorBG01);
    gunnerDel.setForeground(colorFG01);
    gunnerDel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    tankStats1.setForeground(colorFG01);
    tankStats2.setForeground(colorFG01);
    tankStats3.setForeground(colorFG01);
    tankDelPanel.setBackground(colorBG01);
    tankDelPanel.setForeground(colorFG01);
    tankDel.setBackground(colorBG01);
    tankDel.setForeground(colorFG01);
    tankDel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    rocketStats1.setForeground(colorFG01);
    rocketStats2.setForeground(colorFG01);
    rocketStats3.setForeground(colorFG01);
    rocketDelPanel.setBackground(colorBG01);
    rocketDelPanel.setForeground(colorFG01);
    rocketDel.setBackground(colorBG01);
    rocketDel.setForeground(colorFG01);
    rocketDel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));

    bottom.setBackground(colorBG01);
    bottom.setForeground(colorFG01);
    bottom.setBorder(BorderFactory.createMatteBorder(0, 2, 4, 8, colorBG01));
    readyUp.setBackground(colorBG01);
    readyUp.setForeground(colorFG01);
    readyUp.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    cancel.setBackground(colorBG01);
    cancel.setForeground(colorFG01);
    cancel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));




  }

  protected void setMyInfo(String string) {
    myInfo.setText("      [" + string + "]");


  }

  protected void setEnemyInfo(String enemy, String otherReady) {
    String t = "[" + enemy + "]      ";


    if (enemy.equals("")) {
      t = "Awaiting Other Player...      ";
      enablePreGame();


    }
    enemyName = enemy;
    enemyInfo.setText(t);


    if (otherReady.equals("true")) {
      otherReady();


    }
  }

  protected void addUnit(String id, String type, String worth, String health, String energy, String vision, String accuracy) throws IOException {
    PICAIURecruittedUnit unit = new PICAIURecruittedUnit(Integer.parseInt(id), Integer.parseInt(type), Integer.parseInt(worth),
            Integer.parseInt(health), Integer.parseInt(energy), Integer.parseInt(vision), Integer.parseInt(accuracy),null);


    String team = "red";


    if (!parent.getClient().isRedTeam) {
      team = "blue";
    }

    //add gunner
    if (type.equals("0")) {
      //gunners[gunnersNum++] = unit;
      gunnerMen.put(unit.getID(), unit);
      //JPanel p = new JPanel();
      //for (int i = 0; i < gunnersNum; i++) {
      ImageIcon icon = new ImageIcon(ImageIO.read(new File("res/images/icon_" + team + "_gunner.gif")));
      JLabel l = new JLabel(icon);
      JPanel p = new JPanel(new FlowLayout());
      p.removeAll();
      p.add(l);
      p.setToolTipText("" + unit.getID());


      ((FlowLayout) ((JPanel) p).getLayout()).setVgap(1);


      ((FlowLayout) ((JPanel) p).getLayout()).setHgap(1);
      gunnerVec.addElement(p);
      //gunnerImgs.setModel(new DefaultListModel());
      gunnerImgs.setListData(gunnerVec);
      //gunnerScrollPane.validate();
      //add tank


    } else if (type.equals("1")) {
      //tanks[tanksNum++] = unit;
      tankMen.put(unit.getID(), unit);
      ImageIcon icon = new ImageIcon(ImageIO.read(new File("res/images/icon_" + team + "_tank.gif")));
      JLabel l = new JLabel(icon);
      JPanel p = new JPanel(new FlowLayout());
      p.removeAll();
      p.add(l);
      p.setToolTipText("" + unit.getID());


      ((FlowLayout) ((JPanel) p).getLayout()).setVgap(1);


      ((FlowLayout) ((JPanel) p).getLayout()).setHgap(1);
      tankVec.addElement(p);
      //gunnerImgs.setModel(new DefaultListModel());
      tankImgs.setListData(tankVec);
      //add rocketeer


    } else if (type.equals("2")) {
      //rocketeers[rocketeersNum++] = unit;
      rocketMen.put(unit.getID(), unit);
      ImageIcon icon = new ImageIcon(ImageIO.read(new File("res/images/icon_" + team + "_rocket.gif")));
      JLabel l = new JLabel(icon);
      JPanel p = new JPanel(new FlowLayout());
      p.removeAll();
      p.add(l);
      p.setToolTipText("" + unit.getID());


      ((FlowLayout) ((JPanel) p).getLayout()).setVgap(1);


      ((FlowLayout) ((JPanel) p).getLayout()).setHgap(1);
      rocketVec.addElement(p);
      //gunnerImgs.setModel(new DefaultListModel());
      rocketImgs.setListData(rocketVec);


    }
  }

  void otherReady() {
    this.enemyInfo.setText("[" + enemyName + "] is ready!!      ");


  }

  private void clearUnits() {
    gunnerVec.clear();
    rocketVec.clear();
    tankVec.clear();
    gunnerStats1.setText("");
    gunnerStats2.setText("");
    gunnerStats3.setText("");
    tankStats1.setText("");
    tankStats2.setText("");
    tankStats3.setText("");
    rocketStats1.setText("");
    rocketStats2.setText("");
    rocketStats3.setText("");
    gunnerMen.clear();
    tankMen.clear();
    rocketMen.clear();


  }

  void removeUnitFromPreGame(String id, String type) {
    int index = 0;
    if (type.equals("0")) {
      index = gunnerImgs.getSelectedIndex();
      PICAIURecruittedUnit u = gunnerMen.get(id);
      for (Object j : gunnerVec) {
        JPanel jp = (JPanel) j;
        if (jp.getToolTipText().equals("" + id)) {
          gunnerVec.remove(j);
          break;
        }
      }
      gunnerMen.remove(id);
      gunnerStats1.setText("");
      gunnerStats2.setText("");
      gunnerStats3.setText("");
      gunnerImgs.clearSelection();
      gunnerImgs.setListData(gunnerVec);
      //gunnerImgs.setSelectedIndex(index);

    } else if (type.equals("1")) {
      index = tankImgs.getSelectedIndex();
      PICAIURecruittedUnit u = tankMen.get(id);
      for (Object j : tankVec) {
        JPanel jp = (JPanel) j;
        if (jp.getToolTipText().equals("" + id)) {
          tankVec.remove(j);
          break;
        }
      }
      tankMen.remove(id);
      tankStats1.setText("");
      tankStats2.setText("");
      tankStats3.setText("");
      tankImgs.clearSelection();
      tankImgs.setListData(tankVec);
      //tankImgs.setSelectedIndex(index);

    } else if (type.equals("2")) {
      index = rocketImgs.getSelectedIndex();
      PICAIURecruittedUnit u = rocketMen.get(id);
      for (Object j : rocketVec) {
        JPanel jp = (JPanel) j;
        if (jp.getToolTipText().equals("" + id)) {
          rocketVec.remove(j);
          break;
        }
      }
      rocketMen.remove(id);
      rocketStats1.setText("");
      rocketStats2.setText("");
      rocketStats3.setText("");
      rocketImgs.clearSelection();
      rocketImgs.setListData(rocketVec);
      //rocketImgs.setSelectedIndex(index);
    }
  }

  void clearPreGame() {
    clearUnits();
    enablePreGame();
  }
}

class JListWithImages extends JList implements MouseListener {

  public int id = -1;
  public int index;
  private MouseEvent myCachedEvent;

  public JListWithImages() {
    super(new DefaultListModel());

    addMouseListener(this);
    setCellRenderer(new CustomCellRenderer());
    this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    this.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    this.setVisibleRowCount(-1);
    this.setDragEnabled(false);
    //this.setFixedCellWidth(90);
    addListSelectionListener(
            new ListSelectionListener() {

              public void valueChanged(ListSelectionEvent e) {
              }
            });
  }

  DefaultListModel getContents() {
    return (DefaultListModel) getModel();
  }

  public void mouseClicked(MouseEvent e) {
  }

  public void mousePressed(MouseEvent e) {
  }

  public void mouseReleased(MouseEvent e) {
    // index = -1;
  }

  public void mouseEntered(MouseEvent e) {
  }

  public void mouseExited(MouseEvent e) {
  }

  class CustomCellRenderer implements ListCellRenderer {

    public Component getListCellRendererComponent(JList list, Object value, int index,
            boolean isSelected, boolean cellHasFocus) {
      Component component = (Component) value;
      component.setBackground(isSelected ? Color.gray : Color.white);
      component.setForeground(isSelected ? Color.white : Color.gray);
      return component;
    }
  }

  @Override
  protected void fireSelectionValueChanged(int firstIndex,
          int lastIndex,
          boolean isAdjusting) {
    // now continue with update of whoever is listening to list for selection changes
    super.fireSelectionValueChanged(firstIndex, lastIndex, isAdjusting);
    // if the selection occurred and we have cached the event,
    // launch a copy of the cached event so dragging can occur right
    // away, if necessary
    if (myCachedEvent != null) {
      super.processMouseEvent(new MouseEvent(
              (Component) myCachedEvent.getSource(),
              myCachedEvent.getID(),
              myCachedEvent.getWhen(),
              myCachedEvent.getModifiersEx(),
              myCachedEvent.getX(),
              myCachedEvent.getY(),
              myCachedEvent.getClickCount(),
              myCachedEvent.isPopupTrigger()));
      myCachedEvent = null;
    }
  }  // fireSelectionValueChanged()

  /**
   * overriding so we can cache the mouse event
   * @param e is the invoking event
   */
  @Override
  protected void processMouseEvent(MouseEvent e) {
    int modifiers = e.getModifiersEx();

    // if clicked with left button, cache the event
    if ((modifiers & e.BUTTON1_DOWN_MASK) != 0) {
      myCachedEvent = e;
    }

    // move ahead and do normal processing on event
    super.processMouseEvent(e);
  }  // processMouseEvent()
}
